var Dao = Class.create({
	initialize: function() {
		var dbName = "dbOpenHours";
		var dbVersion = "1.0.0";
		var dbDisplayName = "Open Hours Database";
		var dbSize = 200000;
		this.db = openDatabase(dbName, dbVersion, dbDisplayName, dbSize);
	},
	
	createEntriesTable: function() {
		if (!this.db) {
			Mojo.Log.error("Open Hours database is not available");
		} else {
			this.db.transaction((function(transaction) {
				var sql = "DROP TABLE Entries;";
				transaction.executeSql(sql, [], this.dbSuccessHandler.bind(this), this.dbErrorHandler.bind(this));
				
				var sql = "CREATE TABLE IF NOT EXISTS Entries (entryId INTEGER PRIMARY KEY AUTOINCREMENT,description TEXT NOT NULL,location TEXT NULL,phone TEXT NULL,availability TINYINT NOT NULL,isOpenDay1 INTEGER NOT NULL,timeOpenDay1 TEXT NOT NULL,timeCloseDay1 TEXT NOT NULL,isOpenDay2 INTEGER NOT NULL,timeOpenDay2 TEXT NOT NULL,timeCloseDay2 TEXT NOT NULL,isOpenDay3 INTEGER NOT NULL,timeOpenDay3 TEXT NOT NULL,timeCloseDay3 TEXT NOT NULL,isOpenDay4 INTEGER NOT NULL,timeOpenDay4 TEXT NOT NULL,timeCloseDay4 TEXT NOT NULL,isOpenDay5 INTEGER NOT NULL,timeOpenDay5 TEXT NOT NULL,timeCloseDay5 TEXT NOT NULL,isOpenDay6 INTEGER NOT NULL,timeOpenDay6 TEXT NOT NULL,timeCloseDay6 TEXT NOT NULL,isOpenDay7 INTEGER NOT NULL,timeOpenDay7 TEXT NOT NULL,timeCloseDay7 TEXT NOT NULL);";
				transaction.executeSql(sql, [], this.dbSuccessHandler.bind(this), this.dbErrorHandler.bind(this));
				
				var sql = "CREATE INDEX Index_Entries_entryId ON Entries(entryId);";
				transaction.executeSql(sql, [], this.dbSuccessHandler.bind(this), this.dbErrorHandler.bind(this));
				
				var sql = "CREATE INDEX Index_Entries_location ON Entries(location);";
				transaction.executeSql(sql, [], this.dbSuccessHandler.bind(this), this.dbErrorHandler.bind(this));
			}).bind(this));
		}
	},
	
	insertSampleRecords: function() {
		if (!this.db) {
			Mojo.Log.error("Open Hours database is not available");
		} else {
			this.db.transaction((function(transaction) {
				var sql = "INSERT INTO Entries (description,location,phone,availability,isOpenDay1,timeOpenDay1,timeCloseDay1,isOpenDay2,timeOpenDay2,timeCloseDay2,isOpenDay3,timeOpenDay3,timeCloseDay3,isOpenDay4,timeOpenDay4,timeCloseDay4,isOpenDay5,timeOpenDay5,timeCloseDay5,isOpenDay6,timeOpenDay6,timeCloseDay6,isOpenDay7,timeOpenDay7,timeCloseDay7) VALUES ('Paradise Hair And Tan Salon','Boonville, IN','8128977800',1,0,'1984-01-01 08:00:00','1984-01-01 17:00:00',1,'1984-01-02 09:00:00','1984-01-02 18:00:00',1,'1984-01-03 11:00:00','1984-01-03 20:30:00',0,'1984-01-04 08:00:00','1984-01-04 17:00:00',1,'1984-01-05 13:00:00','1984-01-05 21:00:00',1,'1984-01-06 09:30:00','1984-01-06 15:45:00',1,'1984-01-07 10:00:00','1984-01-07 14:30:00');";
				transaction.executeSql(sql, [], this.dbSuccessHandler.bind(this), this.dbErrorHandler.bind(this));

				var sql = "INSERT INTO Entries (description,location,phone,availability,isOpenDay1,timeOpenDay1,timeCloseDay1,isOpenDay2,timeOpenDay2,timeCloseDay2,isOpenDay3,timeOpenDay3,timeCloseDay3,isOpenDay4,timeOpenDay4,timeCloseDay4,isOpenDay5,timeOpenDay5,timeCloseDay5,isOpenDay6,timeOpenDay6,timeCloseDay6,isOpenDay7,timeOpenDay7,timeCloseDay7) VALUES ('Guitar Center','Evansville, IN',null,1,1,'1984-01-01 12:00:00','1984-01-01 17:00:00',1,'1984-01-02 10:00:00','1984-01-02 21:00:00',1,'1984-01-03 10:00:00','1984-01-03 21:00:00',1,'1984-01-04 10:00:00','1984-01-04 21:00:00',1,'1984-01-05 10:00:00','1984-01-05 21:00:00',1,'1984-01-06 10:00:00','1984-01-06 20:00:00',1,'1984-01-07 10:00:00','1984-01-07 20:00:00');";
				transaction.executeSql(sql, [], this.dbSuccessHandler.bind(this), this.dbErrorHandler.bind(this));

				var sql = "INSERT INTO Entries (description,location,phone,availability,isOpenDay1,timeOpenDay1,timeCloseDay1,isOpenDay2,timeOpenDay2,timeCloseDay2,isOpenDay3,timeOpenDay3,timeCloseDay3,isOpenDay4,timeOpenDay4,timeCloseDay4,isOpenDay5,timeOpenDay5,timeCloseDay5,isOpenDay6,timeOpenDay6,timeCloseDay6,isOpenDay7,timeOpenDay7,timeCloseDay7) VALUES ('Guitar Center','Louisville, KY',null,1,1,'1984-01-01 12:00:00','1984-01-01 17:00:00',1,'1984-01-02 10:00:00','1984-01-02 21:00:00',1,'1984-01-03 10:00:00','1984-01-03 21:00:00',1,'1984-01-04 10:00:00','1984-01-04 21:00:00',1,'1984-01-05 10:00:00','1984-01-05 21:00:00',1,'1984-01-06 10:00:00','1984-01-06 20:00:00',1,'1984-01-07 10:00:00','1984-01-07 20:00:00');";
				transaction.executeSql(sql, [], this.dbSuccessHandler.bind(this), this.dbErrorHandler.bind(this));

				var sql = "INSERT INTO Entries (description,location,phone,availability,isOpenDay1,timeOpenDay1,timeCloseDay1,isOpenDay2,timeOpenDay2,timeCloseDay2,isOpenDay3,timeOpenDay3,timeCloseDay3,isOpenDay4,timeOpenDay4,timeCloseDay4,isOpenDay5,timeOpenDay5,timeCloseDay5,isOpenDay6,timeOpenDay6,timeCloseDay6,isOpenDay7,timeOpenDay7,timeCloseDay7) VALUES ('Guitar Center','Terre Haute, IN',null,1,1,'1984-01-01 12:00:00','1984-01-01 17:00:00',1,'1984-01-02 10:00:00','1984-01-02 21:00:00',1,'1984-01-03 10:00:00','1984-01-03 21:00:00',1,'1984-01-04 10:00:00','1984-01-04 21:00:00',1,'1984-01-05 10:00:00','1984-01-05 21:00:00',1,'1984-01-06 10:00:00','1984-01-06 20:00:00',1,'1984-01-07 10:00:00','1984-01-07 20:00:00');";
				transaction.executeSql(sql, [], this.dbSuccessHandler.bind(this), this.dbErrorHandler.bind(this));

				var sql = "INSERT INTO Entries (description,location,phone,availability,isOpenDay1,timeOpenDay1,timeCloseDay1,isOpenDay2,timeOpenDay2,timeCloseDay2,isOpenDay3,timeOpenDay3,timeCloseDay3,isOpenDay4,timeOpenDay4,timeCloseDay4,isOpenDay5,timeOpenDay5,timeCloseDay5,isOpenDay6,timeOpenDay6,timeCloseDay6,isOpenDay7,timeOpenDay7,timeCloseDay7) VALUES ('South Side Bar','Boonville, IN',NULL,1,1,'1984-01-01 12:00:00','1984-01-01 23:30:00',1,'1984-01-02 10:00:00','1984-01-03 01:00:00',1,'1984-01-03 10:00:00','1984-01-04 01:00:00',1,'1984-01-04 10:00:00','1984-01-05 01:00:00',1,'1984-01-05 10:00:00','1984-01-06 01:00:00',1,'1984-01-06 10:00:00','1984-01-07 03:30:00',1,'1984-01-07 11:00:00','1984-01-08 03:30:00');";
				transaction.executeSql(sql, [], this.dbSuccessHandler.bind(this), this.dbErrorHandler.bind(this));
			}).bind(this));
		}
	},
	
	deleteEntry: function(entryId) {
		if (!this.db) {
			Mojo.Log.error("Open Hours database is not available");
		} else {
			this.db.transaction((function(transaction) {
				var sql = "DELETE FROM Entries WHERE entryId = ?;";
				transaction.executeSql(sql, [entryId], this.dbSuccessHandler.bind(this), this.dbErrorHandler.bind(this));
			}).bind(this));
		}
	},
	
	dbSuccessHandler: function(transaction, results) {
		Mojo.Log.info("Successful SQL execution");
	},
	
	dbErrorHandler: function(transaction, error) {
		Mojo.Log.error("SQL ERROR: " + error.message);
	}
});

